// created by jimmy 2016/2/29
var registerType;
var url;
$(document).ready(function() {

    registerType = getURLInformation().type;
    url = getURLInformation().url;
    var obj = $(".login-main").attr("data-a");
    if (obj != "undefined") {
        setError($.parseJSON(obj));
    }

    createCaptcha($(".get-pic-code"));

    if (registerType == 2) { //忘记密码
        $(".header-text").text("忘记密码");
        $(".btn-register").val("确认提交");
        $(".register-text").css("display", "none");
        $(".login-content").attr("data-tag", "forget");
        $(".login-content").attr("action", "/register/register?type=2");

    } else {
        $(".login-content").attr("action", "/register/register?type=4");

        $(".login-content").attr("data-tag", "register");
        $("#register").attr("href", "");
        $("#register").css("cursor", "default");

        $(".register-free").attr("href", "");
        $(".register-free").css("cursor", "default");

        $("#login").css("display", "none");
    }
    passwordInputListener($(".pswInput2"));
    passwordInputListener($(".login-content").find(".pswInput"));
    phoneInputListener($(".login-content").find(".phoneInput"));
    codeInputListener($(".login-content").find(".codeInput"));
    codeInputListener($(".login-content").find(".codePicInput"));
    $("#myOrder").css("display", "none");
    $("#register").css("display", "none");
    $(".ver-line").css("display", "none");

    var back = function() {
        window.location.reload();
    }
    setLoginCallBack(back);
});

$(".btn-register").click(function(e) {
    e.preventDefault();
    var fa = $(this).parent();
    if (!checkPhone(fa.find(".phoneInput")))
        return;
    if (!checkCode(fa.find(".codeInput")))
        return;
     if (!checkCode(fa.find(".codePicInput")))
        return;
    if (!checkPassword(fa.find(".pswInput")))
        return;
    if (!checkPassword(fa.find(".pswInput2")))
        return;
    if (fa.find(".pswInput2").prop("value") != fa.find(".pswInput").prop("value")) {
        showErrorTips(fa.find(".errorPswTips2"), "两次密码不一致");
        return;
    }

    $(this).parent()[0].submit();
});

function setError(datas) {

    $(".login-content").find(".phoneInput").val(datas.phone);

    if ($(".login-content").find(".phoneInput").val().length == 11) {
        $(".login-content").find(".phoneInput").attr('data-flag', '1');
    }
    $(".login-content").find(".codeInput").attr('data-flag', '1');
    checkPhoneReq($(".login-content").find(".phoneInput"), checkPhoneReqCallback);

    checkLoginBtn($(".login-content").find(".phoneInput"));

    if (datas.status == "0201") {
        showErrorTips($(".errorPhoneTips"), "手机未注册");
    } else if (datas.status == "1011") {
        showErrorTips($(".errorPicCodeTips"), "图片验证码错误");
    }else if (datas.status == "0202") {
        showErrorTips($(".errorPswTips"), "密码不能少于6位");
    } else if (datas.status == "0222") {
        showErrorTips($(".errorCodeTips"), "验证码错误");
    }
}
